<template>
  <div id="header">
    <div class="left">
      <i
        :class="['iconfont', 'icon-menu', {collapse: menuCollapse}]"
        @click="$store.commit('layout/menuCollapseToggle')"
        title="收起/展开导航菜单"
      ></i>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item
          v-for="b in breadcrumb"
          :key="b.title"
          :to="b.url"
          @click.native="breadClick(b.url)"
        >{{b.title}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="right">
      <i class="el-icon-rank" @click="fullscreenToggle" title="全屏"></i>
      <el-dropdown class="faceAndMenu" @command="handleCommand">
        <span class="el-dropdown-link">
          <div class="face">
            <i class="iconfont icon-user"></i>
          </div>
          <span class="username">{{$store.state.user.nickname}}</span>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="center">密码修改</el-dropdown-item>
          <el-dropdown-item command="logout">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="25%" :before-close="close">
      <el-form :model="form">
        <el-form-item label="密码" label-width="100px">
          <el-input v-model="form.passwd" show-password type="password"></el-input>
        </el-form-item>
        <el-form-item label="再次输入" label-width="100px">
          <el-input v-model="form.repasswd" show-password type="password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import screenfull from "screenfull";
export default {
  data() {
    return {
      form: {
        passwd: "",
        repasswd: ""
      },
      dialogFormVisible: false,
      inputWidth: "200px"
    };
  },
  methods: {
    submit() {
      this.$api
        .put(
          "/user/password/",
          {
            userid: this.$store.state.user.userId,
            password: this.$md5(this.form.passwd)
          },
          { autoAlert: false, returnAll: true }
        )
        .then(res => {
          if (res.code == 200) {
            this.dialogFormVisible = false;
            this.$message({
              type: "success",
              message: "更改成功"
            });
          } else {
            this.$message({
              type: "error",
              message: "失败"
            });
          }
        })
        .catch(err => {
          this.$message({
            type: "warning",
            message: err
          });
        })
        .finally(res => {});
    },
    close() {
      this.dialogFormVisible = false;
      this.form = { passwd: "", repasswd: "" };
    },
    fullscreenToggle() {
      screenfull.toggle();
    },
    breadClick(url) {
      console.log(this.breadcrumb);
      if (url != "") this.$store.commit("layout/clickTab", url);
    },
    handleCommand(command) {
      switch (command) {
        case "logout":
          // this.$api.post('auth/logout')
          //   .then(()=>{
          //     this.$router.replace('/login')
          //   })
          localStorage.removeItem("user");
          localStorage.removeItem("token");
          localStorage.removeItem("tabs");
          localStorage.removeItem("indexPath");
          localStorage.removeItem("breadcrumb");
          localStorage.removeItem("auth");
          this.$store.commit("layout/clearStore");
          this.$router.replace("/");
          window.location.reload();
          break;

        case "center":
          this.dialogFormVisible = true;
          break;
      }
    }
  },
  computed: {
    ...mapGetters({
      menuCollapse: "layout/menuCollapse",
      breadcrumb: "layout/breadcrumb"
    })
  }
};
</script>

<style scoped lang="stylus">
@import '~@/stylus/base';

.icon {
  font-size: 24px;
  transition-ease();
  hover-highlight();
}

#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;

  &>div {
    display: flex;
    align-items: center;
  }

  .left {
    .icon-menu {
      @extend .icon;

      &.collapse {
        transform: rotate(90deg);
      }
    }

    .el-breadcrumb {
      margin-left: 30px;
    }
  }

  .right {
    .el-icon-rank {
      @extend .icon;
      transform: rotate(45deg);
    }

    .faceAndMenu {
      margin-left: 20px;

      .el-dropdown-link {
        display: flex;
        align-items: center;
        cursor: pointer;

        &:hover {
          .face, .username {
            color: $color-main;
            border-color: $color-main;
          }
        }

        .face {
          transition-ease();
          border: 1px solid $color-text-placeholder;
          border-radius: 50%;
          width: 40px;
          height: @width;
          display: flex;
          align-items: center;
          justify-content: center;

          i {
            font-size: 26px;
          }
        }

        .username {
          transition-ease();
          margin-left: 10px;
        }
      }
    }
  }
}
</style>
